/**
 * @file barcode.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_BARCODE != 0

#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define BARCODE_WIDTH 220
#define BARCODE_HEIGHT 70

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
static void barcode_prepare_destory(lv_obj_t * activity_obj);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/

lv_obj_t * barcode_app_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        memset(&activity_ext, 0, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_BARCODE;
        activity_ext.create = barcode_app_create;
        activity_ext.prepare_destory = barcode_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_obj_t * content = lv_cont_create(obj, NULL);
    lv_cont_set_fit(content, LV_FIT_NONE);
    lv_obj_set_size(content, BARCODE_WIDTH, BARCODE_HEIGHT);
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_image);
    lv_obj_set_style_local_bg_color(content,LV_OBJ_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_WHITE);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_obj_set_click(content, false);
    lv_obj_align(content, obj, LV_ALIGN_CENTER, 0, -20);

    char * cid = app_adaptor_get_cid_for_barcode();
    lv_obj_t * barcode = barcode_create(content, BARCODE_WIDTH, BARCODE_HEIGHT - 20, cid);
    lv_obj_align(barcode, content, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t * label = lv_label_create(obj, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20);
    lv_label_set_text(label, cid);
    lv_obj_set_click(label, false);
    lv_obj_align(label, content, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    return obj;
}

void barcode_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

        lv_obj_t * obj = barcode_app_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
static void barcode_prepare_destory(lv_obj_t * activity_obj)
{
    lv_watch_png_cache_all_free();
}

#endif /*USE_LV_BARCODE*/
